package pl.pasu.ocrtc;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SharedPreferencesHandler {

    private static SharedPreferences prefs;

    public static void initPreferences(Context context) {
        if (prefs == null) {
            prefs = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
        }
    }

    public static void savePreference(String preferenceKey, String preferenceValue) {
        if (prefs == null) {
            throw new ExceptionInInitializerError("Preferences not initialized.");
        }
        Editor e = prefs.edit();
        e.putString(preferenceKey, preferenceValue);
        e.commit();
    }

    public static void savePreference(String preferenceKey, boolean preferenceValue) {
        if (prefs == null) {
            throw new ExceptionInInitializerError("Preferences not initialized.");
        }
        Editor e = prefs.edit();
        e.putBoolean(preferenceKey, preferenceValue);
        e.commit();
    }

    public static String getStringPreference(String preferenceKey) {
        if (prefs == null) {
            throw new ExceptionInInitializerError("Preferences not initialized.");
        }
        return prefs.getString(preferenceKey, null);
    }

    public static boolean getBooleanPreference(String preferenceKey) {
        if (prefs == null) {
            throw new ExceptionInInitializerError("Preferences not initialized.");
        }
        return prefs.getBoolean(preferenceKey, false);
    }
}
